package timing.ukulele.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import timing.ukulele.data.user.view.ThirdPartyUserVO;
import timing.ukulele.user.mapper.SysThirdPartyUserMapper;
import timing.ukulele.user.persistent.SysThirdPartyUser;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
public class SysThirdPartyUserService extends ServiceImpl<SysThirdPartyUserMapper, SysThirdPartyUser> {
    public IPage<ThirdPartyUserVO> getPage(String username, Long userId, String unionId, String platId, Integer platSource, Boolean deleted, int current, int size) {
        Page<SysThirdPartyUser> page = new Page<>(current, size);
        LambdaQueryWrapper<SysThirdPartyUser> query = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(username))
            query.likeRight(SysThirdPartyUser::getUsername, username);
        if (userId != null)
            query.eq(SysThirdPartyUser::getUserId, userId);
        if (StringUtils.hasText(unionId))
            query.likeRight(SysThirdPartyUser::getUnionId, unionId);
        if (StringUtils.hasText(platId))
            query.likeRight(SysThirdPartyUser::getPlatId, platId);
        if (platSource != null)
            query.eq(SysThirdPartyUser::getPlatSource, platSource);
        if (deleted != null)
            query.eq(SysThirdPartyUser::getDeleted, deleted);
        query.orderByDesc(SysThirdPartyUser::getId);
        IPage<SysThirdPartyUser> iPage = this.getBaseMapper().selectPage(page, query);
        Page<ThirdPartyUserVO> voPage = new Page<>(current, size);
        if (iPage != null && !CollectionUtils.isEmpty(iPage.getRecords())) {
            List<ThirdPartyUserVO> voList = new ArrayList<>(iPage.getRecords().size());
            iPage.getRecords().forEach(po -> {
                ThirdPartyUserVO vo = new ThirdPartyUserVO();
                BeanUtils.copyProperties(po, vo);
                voList.add(vo);
            });
            voPage.setPages(iPage.getPages());
            voPage.setTotal(iPage.getTotal());
            voPage.setRecords(voList);
        }
        return voPage;
    }

    public boolean save(SysThirdPartyUser entity) {
        Date now = new Date();
        entity.setCreateTime(now);
        if (entity.getDeleted() == null) {
            entity.setDeleted(Boolean.FALSE);
        }

        return super.save(entity);
    }

    public boolean saveBatch(Collection<SysThirdPartyUser> entityList, int batchSize) {
        if (CollectionUtils.isEmpty(entityList)) {
            return false;
        } else {
            Date now = new Date();
            entityList.forEach((entity) -> {
                entity.setCreateTime(now);
                if (entity.getDeleted() == null) {
                    entity.setDeleted(Boolean.FALSE);
                }

            });
            return super.saveBatch(entityList, batchSize);
        }
    }

    public boolean saveOrUpdate(SysThirdPartyUser entity) {
        Date now = new Date();
        if (entity.getId() == null) {
            entity.setCreateTime(now);
            if (entity.getDeleted() == null) {
                entity.setDeleted(Boolean.FALSE);
            }
        }

        return super.saveOrUpdate(entity);
    }

    public boolean saveOrUpdateBatch(Collection<SysThirdPartyUser> entityList) {
        if (CollectionUtils.isEmpty(entityList)) {
            return false;
        } else {
            Date now = new Date();
            entityList.forEach((entity) -> {
                if (entity.getId() == null) {
                    entity.setCreateTime(now);
                    if (entity.getDeleted() == null) {
                        entity.setDeleted(Boolean.FALSE);
                    }
                }

            });
            return super.saveOrUpdateBatch(entityList);
        }
    }

    public boolean saveOrUpdateBatch(Collection<SysThirdPartyUser> entityList, int batchSize) {
        if (CollectionUtils.isEmpty(entityList)) {
            return false;
        } else {
            Date now = new Date();
            entityList.forEach((entity) -> {
                if (entity.getId() == null) {
                    entity.setCreateTime(now);
                    if (entity.getDeleted() == null) {
                        entity.setDeleted(Boolean.FALSE);
                    }
                }

            });
            return super.saveOrUpdateBatch(entityList, batchSize);
        }
    }

    public boolean updateBatchById(Collection<SysThirdPartyUser> entityList, int batchSize) {
        if (CollectionUtils.isEmpty(entityList)) {
            return false;
        } else {
            return super.updateBatchById(entityList, batchSize);
        }
    }
}
